#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

#define MAXS 6561
#define INF 0x7fffffff

using namespace std;

int f[MAXS][2];
int e3[9] = {1,3,9,27,81,243,729,2187,6561};
int v[9] = {0,9,6,8,8,8,8,6,9};
bool calced[MAXS][2];

int dp(int now,int who) {

	if (calced[now][who]) return f[now][who];
	int tmp[10],S = now,next,score = 0,fact = who ? 1 : 2;
	tmp[0] = tmp[9] = -1;

	f[now][who] = who ? INF : -INF;
	for (int j = 8; j >= 1; j--)
	{
		tmp[j] = S % 3;
		S /= 3;
	}

	bool flag = 0;
	for (int j = 1; j <= 8; j++)
	{
		if (!tmp[j])
		{
			int l,r;
			for (l = j - 1; tmp[l] == who + 1; l--);
			for (r = j + 1; tmp[r] == who + 1; r++);
			if (tmp[l] != fact || l == j - 1) l = 0;
			if (tmp[r] != fact || r == j + 1) r = 0;
			if (l || r)
			{
				flag = 1;
				if (l) for (int t = l + 1; t < j; t++) tmp[t] = fact;
				if (r) for (int t = j + 1; t < r; t++) tmp[t] = fact;
				tmp[j] = fact;
				next = 0;
				for (int t = 8,k = 0; t >= 1; t--,k++) next += e3[k] * tmp[t];
				if (!who)
				{
					if (dp(next,1) > f[now][0]) f[now][0] = f[next][1];
				}
				else if (dp(next,0) < f[now][1]) f[now][1] = f[next][0];
				tmp[j] = 0;
				if (l) for (int t = l + 1; t < j; t++) tmp[t] = 3 - fact;
				if (r) for (int t = j + 1; t < r; t++) tmp[t] = 3 - fact;
			}
		}
		else if (tmp[j] == 1) score -= v[j];
		else score += v[j];
	}
	if (!flag) f[now][who] = score;
	calced[now][who] = 1;
	return f[now][who];
}

int main() {

	freopen("dp.out","w",stdout);
	memset(calced,0,sizeof(calced));
	for (int i = 0; i < MAXS; i++)
	{
		dp(i,0);
		dp(i,1);
		int tmp[9],S = i;
		for (int j = 8; j >= 1; j--)
		{
			tmp[j] = S % 3;
			S /= 3;
		}
		for (int j = 1; j <= 8; j++) printf("%d",tmp[j]);
		printf("		%d		%d\n",f[i][0],f[i][1]);

	}
	return 0;
}
